Skip to content

[Go] Generate type aliases for deduplicated inline model schemas#22694

Open
AriehSchneier wants to merge 11 commits intoOpenAPITools:masterfrom
AriehSchneier:go-codegen-add-alias
Open

[Go] Generate type aliases for deduplicated inline model schemas#22694
AriehSchneier wants to merge 11 commits intoOpenAPITools:masterfrom
AriehSchneier:go-codegen-add-alias

Conversation

@AriehSchneier
Copy link
Copy Markdown

@AriehSchneier AriehSchneier commented Jan 14, 2026

Description

When the InlineModelResolver deduplicates inline schemas that share the same structure, the Go code generator now creates type aliases using the original inline model names, pointing to the shared deduplicated type.

Example

Given an OpenAPI spec with two different models that happen to have identically-structured inline schemas:

BlogPost:
  properties:
    author:
      type: object
      properties:
        name:
          type: string

Recipe:
  properties:
    ingredients:
      type: object
      properties:
        name:
          type: string

Before this PR:

The second inline schema would reuse the first deduplicated type, losing context:

type BlogPost struct {
    Author BlogPostAuthor  `json:"author,omitempty"`
}

type Recipe struct {
    Ingredients BlogPostAuthor  `json:"ingredients,omitempty"`  // Wrong context!
}

After this PR:

Each field uses its own alias, preserving the original naming context:

type BlogPost struct {
    Author BlogPostAuthor  `json:"author,omitempty"`
}

type RecipeIngredients = BlogPostAuthor  // alias to deduplicated type

type Recipe struct {
    Ingredients RecipeIngredients  `json:"ingredients,omitempty"`  // Correct context!
}

Implementation Details

  1. InlineModelResolver: Sets vendor extension x-alias-name when deduplicating schemas
  2. DefaultCodegen: Reads the vendor extension and sets CodegenProperty.dataTypeAlias (language-neutral)
  3. AbstractGoCodegen:
    • Swaps dataType and dataTypeAlias in postProcessModels() so struct fields use the alias names
    • Collects type aliases and stores them in model vendor extensions
    • Updates array types to use alias names (e.g., []RecipeIngredients)
  4. model_simple.mustache: Renders type alias definitions at the top of the file

Benefits

  • Preserves original naming context from the OpenAPI specification
  • Makes generated code more readable and maintainable when unrelated models share identical inline schema structures
  • Works for both ObjectSchemas and MapSchemas (additionalProperties: true)
  • Array types correctly use aliases (e.g., []RecipeIngredients instead of []BlogPostAuthor)
  • Generic code remains language-neutral; only Go generator affected

Testing

Added comprehensive test typeAliasForDeduplicatedInlineSchemasTest covering:

  • Direct properties with deduplicated schemas
  • Array items with deduplicated schemas
  • MapSchema (additionalProperties: true) cases
  • Verification that aliases are generated and used correctly

Configuration Option

A configuration option generateTypeAliasesForDedupedSchemas has been added (defaults to true). Users who prefer the old behavior can disable this feature:

--additional-properties=generateTypeAliasesForDedupedSchemas=false

This provides backward compatibility while enabling the improved naming by default.


Summary by cubic

Generates Go type aliases for deduplicated inline model schemas so fields keep their original, contextual names. Adds a generateTypeAliasesForDedupedSchemas option (default: true) and wires it into CLI, docs, and tests.

  • New Features
    • InlineModelResolver tags deduped inline schemas with x-alias-name (respects inlineSchemaNameMapping).
    • DefaultCodegen sets CodegenProperty.dataTypeAlias; updates equality/hash/toString.
    • Go generator swaps alias names into fields and arrays, collects aliases, and emits type A = B in model_simple.mustache; gated by generateTypeAliasesForDedupedSchemas via CLI/additional-properties.
    • Supports object and map (additionalProperties: true) schemas; direct $ref does not create aliases.
    • Docs updated (docs/generators/go.md); option exposed in GoClientCodegen; tests cover resolver alias tagging/mapping, direct properties, arrays, maps, the CLI option; sample updated.

Written for commit f5db99c. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java:195">
P2: `dataTypeAlias` was added but omitted from equals/hashCode/toString, so properties differing only by alias compare equal and hash the same</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@AriehSchneier
Copy link
Copy Markdown
Author

AriehSchneier commented Jan 14, 2026

@cubic-dev-ai please re-run a review.

@cubic-dev-ai
Copy link
Copy Markdown
Contributor

cubic-dev-ai Bot commented Jan 14, 2026

@cubic-dev-ai tplease re-run a review.

@AriehSchneier I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai
Copy link
Copy Markdown
Contributor

cubic-dev-ai Bot commented Jan 14, 2026

@cubic-dev-ai please re-run a review.

@AriehSchneier I have started the AI code review. It will take a few minutes to complete.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Alias vendor extension can be overwritten; deduplication alias is lost if source schema already defines x-alias-name.</violation>

<violation number="2" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Alias metadata is added only for dedup via makeSchemaInComponents; other schema reuse paths still drop x-alias-name, so deduplicated inline schemas may miss alias information.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Deduplicated inline schema alias ignores inlineSchemaNameMapping by storing the unmapped name in x-alias-name</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:670">
P2: x-alias-name for deduplicated inline schemas bypasses inlineSchemaNameMapping, producing unmapped alias names</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@AriehSchneier
Copy link
Copy Markdown
Author

Go Maintainers (@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5) any chance you could take a look and tell me what you think?

@AriehSchneier
Copy link
Copy Markdown
Author

@wing328 I noticed you had kicked off the tests (which showed a failure), I fixed the test, any chance you can kick them off for me again?

@AriehSchneier
Copy link
Copy Markdown
Author

Go Maintainers (@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5) any chance you could take a look and tell me what you think?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Feb 24, 2026

@AriehSchneier thanks for the PR

if i'm not mistaken, the issue has to do with the option skipSchemaReuse but this PR doesn't fix it.

shouldn't we fix the option instead?

@AriehSchneier
Copy link
Copy Markdown
Author

@wing328 I think this should be done as well as the fix for skipSchemaReuse.

I wanted to turn on skipSchemaReuse because the names of the types where they were reused made no sense in my case. However by giving the type an alias where it is reused I no longer need to turn on skipSchemaReuse. (But that doesn't mean the other fix shouldn't be done, I just personally wont need it as I wont be turning on skipSchemaReuse).

In short this PR is not a fix for that bug, it is an enhancement which means I won't be using that feature and therefore will not personally be hitting that bug.

(I hope that makes sense?)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Feb 24, 2026

if we fix skipSchemaReuse, would that meet your requirement?

@AriehSchneier
Copy link
Copy Markdown
Author

if we fix skipSchemaReuse, would that meet your requirement?

I would prefer this change over a fix for the above bug.

Also note, this change would be useful for any language that has alias types, if you are more familiar with another language I could try to add that language in this PR.

@rubenhoenle
Copy link
Copy Markdown
Contributor

if we fix skipSchemaReuse, would that meet your requirement?

@wing328 Maybe I got something completely wrong, but skipSchemaReuse seems to work somehow, see my comment in the GitHub issue with the bug report: #22632 (comment)

@AriehSchneier
Copy link
Copy Markdown
Author

if we fix skipSchemaReuse, would that meet your requirement?

@wing328 Maybe I got something completely wrong, but skipSchemaReuse seems to work somehow, see my comment in the GitHub issue with the bug report: #22632 (comment)

You are correct, that wasn't actually a bug, I have closed that issue and removed the link from this PR.

However I would still like this enhancement to proceed.

AriehSchneier and others added 2 commits April 17, 2026 12:57
Based on review feedback:
1. Improved template comment to be more specific about "deduplicated inline model schemas"
2. Added configuration option 'generateTypeAliasesForDedupedSchemas' (defaults to true)
   - Added constant to CodegenConstants
   - Added boolean field to AbstractGoCodegen
   - Added CLI option to GoClientCodegen
   - Made type alias generation conditional on this flag
3. Added documentation to Go generator docs explaining the new option

The feature can now be disabled by setting:
--additional-properties=generateTypeAliasesForDedupedSchemas=false

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@AriehSchneier
Copy link
Copy Markdown
Author

Update: Configuration Option Added

To help get this merged, I've added a configuration option generateTypeAliasesForDedupedSchemas that allows users to opt out of the type alias generation if needed.

Default behavior: true (feature enabled - type aliases are generated)
To disable: --additional-properties=generateTypeAliasesForDedupedSchemas=false

This provides backward compatibility while enabling the improved naming context by default. Users who encounter any issues or prefer the old behavior can easily disable the feature without waiting for a new release.

The implementation is complete with:

  • ✅ Configuration constant in CodegenConstants
  • ✅ Boolean field in AbstractGoCodegen
  • ✅ CLI option in GoClientCodegen
  • ✅ Updated documentation in docs/generators/go.md
  • ✅ Feature gated behind the config flag
  • ✅ Comprehensive tests

This should address any concerns about backward compatibility while still providing the benefits of contextual type names for the majority of users.

AriehSchneier and others added 4 commits April 17, 2026 13:26
Added the new configuration option to the test suite:
- Added GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS_VALUE constant
- Added option to createOptions() map
- Added verification in GoClientOptionsTest

This fixes the CI failure where the test was validating that all
CLI options are properly tested.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants